/*!
 Temelia - Heap performance tests.

 Copyright (C) 2008, 2009 Ceata (http://ceata.org/temelia).

 @author Tibi Turbureanu, Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "include/performance.hpp"
#include "include/testutil.hpp"
#include <queue>

extern "C"
{
#include <temelia/heap.h>
}

/* temelia functions */
static void temelia_heap_insert(int n)
{
	heap_t temelia_heap;

	temelia_heap = heap_new(n);

	for (int i = 0; i < n; i++)
		heap_insert(temelia_heap, default_collection[i], int_compare, NULL);

	heap_delete(temelia_heap);
}

static void temelia_heap_remove_min_key(int n)
{
	heap_t temelia_heap;

	temelia_heap = heap_new(n);

	for (int i = 0; i < n; i++)
		heap_insert(temelia_heap, default_collection[i], int_compare, NULL);

	while (!heap_is_empty(temelia_heap))
		heap_remove_min_key(temelia_heap, int_compare, NULL);

	heap_delete(temelia_heap);
}

/* STL functions */
static void stl_priority_queue_push(int n)
{
	std::priority_queue<int> stl_pqueue;

	for (int i = 0; i < n; i++)
		stl_pqueue.push(rand() % n);
}

static void stl_priority_queue_pop(int n)
{
	std::priority_queue<int> stl_pqueue;

	for (int i = 0; i < n; i++)
		stl_pqueue.push(rand() % n);

	for (int i = 0; i < n; i++)
		stl_pqueue.pop();

}

void TemeliaPerformance::pt_heap(void)
{
	TestUnit testUnit("heap");

	GENERATE_TEMELIA_TEST(testUnit, "insert", heap_insert);
	GENERATE_TEMELIA_TEST(testUnit, "remove_min_key", heap_remove_min_key);
	GENERATE_STL_TEST(testUnit, "push", priority_queue_push);
	GENERATE_STL_TEST(testUnit, "pop", priority_queue_pop);

	testUnit.write(fout);
}
